#ifndef eatable_h
#define eatable_h

#include <iostream>

using std::cerr;
using std::cout;
using std::endl;

#include "juce_amalgamated.h"
#include "NutritionalInfo.h"

class Eatable {
protected:

	String name;
	//	String filename;
	String frenchName;
	String description;
	String frenchDescription;

	Image* image;
	NutritionalInfo info;

	static Image* images[23];
public:
	String servingDesc;
	float servingFactor;
	static String groups[23];
	int gid;
	int id;

	enum {
		DAIRY_EGGS,
		SPICES_HERBS,
		BABYFOODS,
		FATS_OILS,
		POULTRY_PRODUCTS,
		SOUPS_SAUCES,
		SAUSAGES_LUNCHEON_MEATS,
		BREAKFAST_CEREALS,
		FRUITS,
		PORK_PRODUCTS,
		VEGETABLES,
		NUTS_SEEDS,
		BEEF_PRODUCTS,
		BEVERAGES,
		SEAFOOD,
		LEGUMES,
		LAMB,
		BAKED_GOODS,
		SWEETS,
		GRAINS_PASTA,
		FAST_FOODS,
		MIXED_DISHES,
		SNACKS,
	};

	Eatable() :
		name(String::empty), image(0), id(0) {

		loadImages();
	}

	Eatable(String name, String filename, NutritionalInfo& info) {
		this->info = info;
		this->name = name;

		File file(filename);
		image = juce::JPEGImageFormat().loadFrom(file);
		if (!image) {
			std::cerr << "Failed to load file " << filename.toUTF8() << std::endl;
		}

		loadImages();
	}

	void setName(string s) {
		const char* text = s.c_str();
		name = String(text);
	}

	void setDescription(string s) {
		const char* text = s.c_str();
		description = String(text);
	}

	void loadImages() {
		static bool ranOnce = false;
		if (!ranOnce) {
			//			images[BREAD] = JPEGImageFormat().loadFrom(File(T("images/foods/bread.jpg")));
			for (int i = 0; i < 23; i++) {
				images[i] = 0;
			}

			images[DAIRY_EGGS] = JPEGImageFormat().loadFrom(File(T("images/foods/eggs.jpg")));
			images[SPICES_HERBS] = JPEGImageFormat().loadFrom(File(T("images/foods/spices.jpg")));
			images[BABYFOODS] = JPEGImageFormat().loadFrom(File(T("images/foods/babyfood.jpg")));
			images[FATS_OILS] = JPEGImageFormat().loadFrom(File(T("images/foods/oils.jpg")));
			images[POULTRY_PRODUCTS] = JPEGImageFormat().loadFrom(File(T("images/foods/poultry.jpg")));
			images[SOUPS_SAUCES] = JPEGImageFormat().loadFrom(File(T("images/foods/soups.jpg")));
			images[SAUSAGES_LUNCHEON_MEATS] = JPEGImageFormat().loadFrom(File(
					T("images/foods/luncheonMeat.jpg")));
			images[BREAKFAST_CEREALS] = JPEGImageFormat().loadFrom(File(T("images/foods/cereal.jpg")));
			images[FRUITS] = JPEGImageFormat().loadFrom(File(T("images/foods/fruit.jpg")));
			images[PORK_PRODUCTS] = JPEGImageFormat().loadFrom(File(T("images/foods/pork.jpg")));
			images[VEGETABLES] = JPEGImageFormat().loadFrom(File(T("images/foods/vegetables.jpg")));
			images[NUTS_SEEDS] = JPEGImageFormat().loadFrom(File(T("images/foods/nuts.jpg")));
			images[BEEF_PRODUCTS] = JPEGImageFormat().loadFrom(File(T("images/foods/beef.jpg")));
			images[BEVERAGES] = JPEGImageFormat().loadFrom(File(T("images/foods/beverage.jpg")));
			images[SEAFOOD] = JPEGImageFormat().loadFrom(File(T("images/foods/shrimp.jpg")));
			images[LEGUMES] = JPEGImageFormat().loadFrom(File(T("images/foods/legumes.jpg")));
			images[LAMB] = JPEGImageFormat().loadFrom(File(T("images/foods/lamb.jpg")));
			images[BAKED_GOODS] = JPEGImageFormat().loadFrom(File(T("images/foods/eggs.jpg")));
			images[SWEETS] = JPEGImageFormat().loadFrom(File(T("images/foods/sweets.jpg")));
			images[GRAINS_PASTA] = JPEGImageFormat().loadFrom(File(T("images/foods/pasta.jpg")));
			images[FAST_FOODS] = JPEGImageFormat().loadFrom(File(T("images/foods/fastfood.jpg")));
			images[MIXED_DISHES] = JPEGImageFormat().loadFrom(File(T("images/foods/mixed.jpg")));
			images[SNACKS] = JPEGImageFormat().loadFrom(File(T("images/foods/snacks.jpg")));

			groups[DAIRY_EGGS] = String(T("Dairy and Eggs"));
			groups[SPICES_HERBS] = String(T("Spices and Herbs"));
			groups[BABYFOODS] = String(T("Baby Foods"));
			groups[FATS_OILS] = String(T("Fats and Oils"));
			groups[POULTRY_PRODUCTS] = String(T("Poultry Products"));
			groups[SOUPS_SAUCES] = String(T("Soups and Sauces"));
			groups[SAUSAGES_LUNCHEON_MEATS] = String(T("Sausages and Lunch Meats"));
			groups[BREAKFAST_CEREALS] = String(T("Breakfast Cereals"));
			groups[FRUITS] = String(T("Fruits"));
			groups[PORK_PRODUCTS] = String(T("Pork Products"));
			groups[VEGETABLES] = String(T("Vegetables"));
			groups[NUTS_SEEDS] = String(T("Nuts and Seeds"));
			groups[BEEF_PRODUCTS] = String(T("Beef Products"));
			groups[BEVERAGES] = String(T("Beverages"));
			groups[SEAFOOD] = String(T("Seafood"));
			groups[LEGUMES] = String(T("Legumes"));
			groups[LAMB] = String(T("Lamb"));
			groups[BAKED_GOODS] = String(T("Baked Goods"));
			groups[SWEETS] = String(T("Sweets"));
			groups[GRAINS_PASTA] = String(T("Grains and Pasta"));
			groups[FAST_FOODS] = String(T("Fast Foods"));
			groups[MIXED_DISHES] = String(T("Mixed Dishes"));
			groups[SNACKS] = String(T("Snacks"));

			//			images[CORN] = JPEGImageFormat().loadFrom(File(T("images/foods/corn.jpg")));
			//			images[EGGPLANT] = JPEGImageFormat().loadFrom(File(T("images/foods/eggplant.jpg")));
			//			images[TEA] = JPEGImageFormat().loadFrom(File(T("images/foods/tea.jpg")));
			//			images[MUSHROOMS] = JPEGImageFormat().loadFrom(File(T("images/foods/mushrooms.jpg")));
			//			images[COFFEE] = JPEGImageFormat().loadFrom(File(T("images/foods/coffee.jpg")));
			//			images[TOMATOES] = JPEGImageFormat().loadFrom(File(T("images/foods/tomatoes.jpg")));
			//			images[BROCCOLI] = JPEGImageFormat().loadFrom(File(T("images/foods/broccoli.jpg")));
			//			images[CROISSANT] = JPEGImageFormat().loadFrom(File(T("images/foods/croissant.jpg")));

			ranOnce = true;
		}
	}

	void setImage(int num) {
		//	void setImage(Image* image) {
		//		this->image = image;

		this->image = images[num];
		//		const char* text = filename.c_str();
		//		String fn(text);
		//		File file(fn);
		//		image = juce::JPEGImageFormat().loadFrom(file);
		//		if (!image) {
		//			std::cerr << "Failed to load file " << fn.toUTF8() << std::endl;
		//		}
	}

	void setInfo(NutritionalInfo& info) {
		this->info = info;
	}

	const String getName() const {
		return name;
	}

	const String getDescription() const {
		return description;
	}

	Image* getImage() const {
		return image;
	}

	NutritionalInfo& getNutritionalInfo() {
		return info;
	}

	void operator=(const Eatable& other) {
		this->id = other.id;
		this->gid = other.gid;
		this->servingDesc = other.servingDesc;
		this->servingFactor = other.servingFactor;
		this->name = other.name;
		this->frenchName = other.frenchName;
		this->description = other.description;
		this->frenchDescription = other.frenchDescription;
		this->image = other.image;
		this->info = other.info;
	}

	String& getServingDesc() {
		return servingDesc;
	}

	float getServingFactor() {
		return servingFactor;
	}

	bool operator<(const Eatable& other) const {
		return name < other.name;
//		return gid > other.gid;
	}
};

#endif
